package org.example.kafka.hello;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.example.kafka.common.CommonConstant;


import java.util.Properties;

/**
 * kafka的生产者
 * @Author:anlu
 */
public class HelloKafkaProducer {

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("bootstrap.servers","127.0.0.1:9092");
        properties.put("key.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");
        KafkaProducer<String,String> kafkaProducer = new KafkaProducer<String, String>(properties);
        try{
            ProducerRecord<String,String> record;
            try{
                record = new ProducerRecord<String,String>(CommonConstant.HELLO_TOPIC,"lesson","01");
                kafkaProducer.send(record);
                System.out.println("消息已发送");
            } catch (Exception e){
                e.printStackTrace();
            }
        }finally {
            kafkaProducer.close();
        }
    }

}
